import torch
import torch.nn as nn 


if __name__ == "__main__":
    t1 = torch.tensor([1, 2], dtype=torch.float32, requires_grad=True)
    t2 = torch.tensor([2, 3], dtype=torch.float32, requires_grad=True)
    z = (t1 + 2 * t2).sum() 
    z.backward()
    print(t1.grad)
    print(t2.grad)

    print("~~~~~~~~~~~")

    t1 = torch.tensor([1, 2], dtype=torch.float32, requires_grad=True)
    t2 = torch.tensor([2, 3], dtype=torch.float32, requires_grad=True)
    z = (t1 + 2 * t2)
    t = (z * z).sum()
    t.backward()
    print(t1.grad)
    print(t2.grad)


    print("~~~~~~~~~~`")

    t1 = torch.tensor([1, 2], dtype=torch.float32, requires_grad=True)
    t2 = torch.tensor([2, 3], dtype=torch.float32, requires_grad=True)
    z = (t1 + 2 * t2.detach())
    t = (z * z).sum()
    t.backward()
    print(t1.grad)
    print(t2.grad)

